<template>
	<a-card :bordered="false" :body-style="{ 'padding-bottom': '0px' }" class="mb-2">
		<a-form ref="formRef" name="advanced_search" :model="searchFormState" class="ant-advanced-search-form">
			<a-row :gutter="24">
				<a-col :span="6">
					<a-form-item label="券名称" name="title">
						<a-input v-model:value="searchFormState.title" allow-clear></a-input>
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-button type="primary" html-type="submit" @click="table.refresh()">查询</a-button>
					<a-button style="margin: 0 8px" @click="() => formRef.resetFields()">重置</a-button>
				</a-col>
			</a-row>
		</a-form>
	</a-card>
	<a-card :bordered="false">
		<s-table
			ref="table"
			:columns="columns"
			:data="loadData"
			:alert="options.alert.show"
			bordered
			:row-key="(record) => record.id"
			:tool-config="toolConfig"
			:row-selection="options.rowSelection"
		>
			<template #operator class="table-operator">
				<a-space>
					<a-button type="primary" @click="form.onOpen()">
						<template #icon><plus-outlined /></template>
						新增卡券
					</a-button>
<!--					<a-button @click="enableBatch()">-->
<!--						启用-->
<!--					</a-button>-->
<!--					<a-button @click="stopBatch()">-->
<!--						停用-->
<!--					</a-button>-->
					<a-button danger @click="removeBatch()">
						删除
					</a-button>
				</a-space>
			</template>
			<template #bodyCell="{ column, record }">
				<template v-if="column.dataIndex === 'couponTime'">
					<div>开始时间：{{ record.startTime }}</div>
					<div>结束时间：{{ record.endTime }}</div>
				</template>
				<template v-if="column.dataIndex === 'condition'">
					{{ record.condition }}元
				</template>
				<template v-if="column.dataIndex === 'value'">
					{{ record.value }}{{ record.valueType === 'DISCOUNT' ? '折' : '元'}}
				</template>
				<template v-if="column.dataIndex === 'verifyType'">
					{{ $TOOL.dictTypeData('COUPON_VERIFY_TYPE', record.verifyType) }}
				</template>
				<template v-if="column.dataIndex === 'couponType'">
					{{ $TOOL.dictTypeData('COUPON_TYPE', record.couponType) }}
				</template>
				<template v-if="column.dataIndex === 'status'">
					<a-tag v-if="record.status === 0" color="blue">
						{{ $TOOL.dictTypeData('COUPON_STATUS', record.status + '') }}
					</a-tag>
					<a-tag v-else-if="record.status === 10" color="green">
						{{ $TOOL.dictTypeData('COUPON_STATUS', record.status + '') }}
					</a-tag>
					<a-tag v-else-if="record.status === 20" color="#f50">
						{{ $TOOL.dictTypeData('COUPON_STATUS', record.status + '') }}
					</a-tag>
					<a-tag v-else-if="record.status === 40" color="orange">
						{{ $TOOL.dictTypeData('COUPON_STATUS', record.status + '') }}
					</a-tag>
					<a-tag v-else-if="record.status === 99" color="red">
						{{ $TOOL.dictTypeData('COUPON_STATUS', record.status + '') }}
					</a-tag>
				</template>
				<template v-if="column.dataIndex === 'action'">
					<a-space>
						<a @click="form.onOpen(record)">编辑</a>
						<a-divider type="vertical" />
						<a @click="form.onOpen(record, true)">详情</a>
						<a-divider type="vertical" />
						<a-dropdown :trigger="['click']">
							<a type="link" @click.prevent>
								更多
								<DownOutlined />
							</a>
							<template #overlay>
								<a-menu>
									<a-menu-item>
										<a-popconfirm title="确定要删除此卡券吗？" @confirm="remove(record)">
											<a-button type="link" danger size="small">删除</a-button>
										</a-popconfirm>
									</a-menu-item>
								</a-menu>
							</template>
						</a-dropdown>
					</a-space>
				</template>
			</template>
		</s-table>
	</a-card>
	<Form ref="form" @successful="table.refresh(true)" />
	<AdjustAccountForm ref="adjustAccountForm" @successful="table.refresh(true)" />
</template>

<script setup name="crmCoupon">
	import { message } from 'ant-design-vue'
	import crmApi from '@/api/crm/crmApi'
	import tool from '@/utils/tool'
	import Form from './form.vue'
	import AdjustAccountForm from "@/views/crm/card/adjustAccountForm.vue";
	let searchFormState = reactive({})
	const formRef = ref()
	const table = ref(null)
	let form = ref()
	let adjustAccountForm = ref()
	const toolConfig = { refresh: true, height: true, columnSetting: true, striped: true }
	const columns = [
		{
			title: '券名称',
			dataIndex: 'title',
		},
		{
			title: '券类型',
			dataIndex: 'couponType',
			width: 120,
			align: 'center'
		},
		{
			title: '核销类型',
			dataIndex: 'verifyType',
			align: 'center',
			width: 100
		},
		{
			title: '使用条件',
			dataIndex: 'useCondition',
			align: 'center',
			width: 100
		},
		{
			title: '券面额/折扣率',
			dataIndex: 'value',
			align: 'center',
			width: 120
		},
		{
			title: '券状态',
			dataIndex: 'status',
			width: 100,
			align: 'center'
		},
		{
			title: '活动时间',
			dataIndex: 'couponTime',
			ellipsis: true,
			sorter: true,
			width: 200
		},
		{
			title: '操作',
			dataIndex: 'action',
			align: 'center'
		}
	]
	let selectedRowKeys = ref([])
	// 列表选择配置
	const options = {
		alert: {
			show: false,
			clear: () => {
				selectedRowKeys = ref([])
			}
		},
		rowSelection: {
			onChange: (selectedRowKey, selectedRows) => {
				selectedRowKeys.value = selectedRowKey
			}
		}
	}
	let cardPropOptions = tool
		.dictTypeList('CARD_PROP')
		.map((item) => {
			return {
				value: item['dictValue'],
				label: item['name']
			}
		})
	let couponVerifyTypeOptions = tool
		.dictTypeList('COUPON_VERIFY_TYPE')
		.map((item) => {
			return {
				value: item['dictValue'],
				label: item['name']
			}
		})
	// 列表数据
	const loadData = (parameter) => {
		return crmApi.couponPage(Object.assign(parameter, searchFormState)).then((res) => {
			return res
		})
	}
	// 删除
	const remove = (record) => {
		let params = [
			{
				id: record.id
			}
		]
		crmApi.couponDelete(params).then(() => {
			table.value.refresh(true)
		})
	}
	// 批量删除
	const removeBatch = () => {
		if (selectedRowKeys.value.length < 1) {
			message.warning('请选择一条或多条数据')
			return false
		}
		const params = selectedRowKeys.value.map((m) => {
			return {
				id: m
			}
		})
		crmApi.couponDelete(params).then(() => {
			table.value.clearRefreshSelected()
		})
	}

	// 批量启用
	const enableBatch = () => {
		if (selectedRowKeys.value.length < 1) {
			message.warning('请选择一条或多条数据')
			return false
		}
		const params = selectedRowKeys.value.map((m) => {
			return {
				id: m
			}
		})
		crmApi.cardEnable(params).then(() => {
			table.value.clearRefreshSelected()
		})
	}

	// 批量停用
	const stopBatch = () => {
		if (selectedRowKeys.value.length < 1) {
			message.warning('请选择一条或多条数据')
			return false
		}
		const params = selectedRowKeys.value.map((m) => {
			return {
				id: m
			}
		})
		crmApi.cardStop(params).then(() => {
			table.value.clearRefreshSelected()
		})
	}
</script>

<style scoped>

</style>
